package com.qujia.station.controller;


import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.page.PageDomain;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.core.page.TableSupport;
import com.qujia.common.utils.StringUtils;
import com.qujia.common.utils.sql.SqlUtil;
import com.qujia.station.domain.Commodity;
import com.qujia.station.service.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Commodity)表控制层
 *
 * @author makejava
 * @since 2023-08-15 14:11:12
 */
@Api("非油品商品列表接口")
@RestController
@RequestMapping("/commodity")
public class CommodityController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private CommodityService commodityService;

    /**
     * 分页查询
     *
     * @param commodity 筛选条件
     * @return 查询结果
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo queryByPage(@RequestBody Commodity commodity) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            commodity.setPageNum(pageNum);
            commodity.setPageSize(pageSize);
            commodity.setOrderBy(orderBy);
        }

        List<Commodity> list= commodityService.query(commodity);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param commodityId 主键
     * @return 单条数据
     */
    @GetMapping("/queryById/{commodityId}")
    @ApiOperation("通过主键查询单条数据")
    public AjaxResult queryById(@PathVariable("commodityId") Long commodityId) {
        return AjaxResult.success(this.commodityService.queryById(commodityId));
    }

    /**
     * 新增数据
     *
     * @param commodity 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation("新增数据")
    public AjaxResult add(@RequestBody Commodity commodity) {
        long timestamp = System.currentTimeMillis();
        commodity.setCommodityCode(timestamp+"");
        return AjaxResult.success(this.commodityService.insert(commodity));
    }
    /**
     * 商品上架
     *
     * @param commodity 实体
     * @return 新增结果
     */
    @PostMapping("/putAway")
    @ApiOperation("商品上架")
    public AjaxResult putAway(@RequestBody Commodity commodity) {
        this.commodityService.putAway(commodity);
        return AjaxResult.success();
    }

    /**
     * 编辑数据
     *
     * @param commodity 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    @ApiOperation("编辑数据")
    public AjaxResult edit(@RequestBody Commodity commodity) {
        return AjaxResult.success(this.commodityService.update(commodity));
    }

    /**
     * 删除数据
     *
     * @param ids 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    @ApiOperation("删除数据")
    public AjaxResult delete(@RequestBody List<Long> ids) {
        return AjaxResult.success(this.commodityService.delete(ids));
    }

}

